<script>
import {employmentType} from "@/constant/employees";
import {mapGetters} from "vuex";

export default {
  name: "empFormDialog",
  props: {
    /* 父组件传递过来的prop用于控制组件的显示和隐藏 */
    showEmpDialog: {
      type: Boolean,
      default: false
    },
    pageParams: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {

      /* 表单数据对象 */
      formData: {
        username: '', // 员工名字
        mobile: '', // 员工手机号
        formOfEmployment: '', // 员工聘用形式(1正式, 2非正式)
        workNumber: '', // 员工工号
        departmentName: '', // 员工所属部门名字
        timeOfEntry: '', // 员工入职日期
        correctionTime: '' // 员工转正日期
      },
      /* 表单规则对象 */
      formRules: {
        username: [
          {required: true, message: '请输入员工姓名', trigger: 'blur'},
        ],
        mobile: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
        ],
        formOfEmployment: [
          {required: true, message: '请选择聘用形式', trigger: 'change'},
        ],
        workNumber: [
          {required: true, message: '请输入员工工号', trigger: 'blur'},
        ],
        departmentName: [
          {required: true, message: '请选择所属部门', trigger: 'change'},
        ],
        timeOfEntry: [
          {required: true, message: '请选择员工入职日期', trigger: 'blur'},
        ],
        correctionTime: [
          {required: true, message: '请选择员工转正日期', trigger: 'blur'},
        ]
      },
      /* 聘用形式 */
      employmentType,
      /* tree组件的展示数据字段 */
      defaultProps: {children: 'children', label: 'name'},
      /* 是否显示tree组件 */
      isShowTree: false
    }
  },
  methods: {
    /* 对话框关闭时的回调函数 */
    handleClose() {
      this.$refs.form.resetFields()
      this.$emit('update:showEmpDialog', false)
    },
    /* 点击取消按钮的回调函数 */
    handleCancel() {
      this.$emit('update:showEmpDialog', false)
    },
    /* 点击确定按钮的回调函数 */
    async handleSubmit() {
      // 兜底校验
      const valid = await this.$refs.form.validate().catch(error => error)
      // 兜底校验失败
      if (!valid) return
      try {
        // 调用addEmployees()派发action发送添加员工的请求
        const message = await this.$store.dispatch('employeesStore/addEmployees', this.formData)
        // 调用fetchEmployees()action发送请求重新获取新的员工列表
        // await this.$store.dispatch('employeesStore/fetchEmployees', this.pageParams)
        // 提示用户添加员工成功
        this.$message.success(message)
        // 通知父组件员工新增成功
        this.$emit('addEmpSuccess')
        // 关闭对话框组件
        // this.$emit('update:showEmpDialog', false)
      } catch (error) {
        // 新增员工失败
        this.$message.error(error.message)
      }
    },
    /*  tree组件节点被点击时的回调函数*/
    handleNodeClick(data) {
      // 判断是否有子节点，如果有则return不执行下面逻辑
      if (data.children.length) return
      // 收集当前点击的tree数据
      this.formData.departmentName = data.name
      // 隐藏tree组件
      this.isShowTree = false
    },
    /* 部门名称输入框获取焦点的回调函数 */
    handleFocus() {
      // 显示tree组件
      this.isShowTree = true
      // 如果已经有部门列表的数据了就不需要再次派发action发送请求
      if (this.treeDepartments.length !== 0) return
      // 调用fetchDepartments()派发action发送请求获取部门列表
      this.$store.dispatch('departmentsStore/fetchDepartments')
    },
    /* 部门名称输入框失去焦点的回调函数 */
    /* handleBlur() {
          setTimeout(() => {
            this.isShowTree = false
          }, 200)
       }*/
  },
  computed: {
    /* 使用mapGetters将departmentsStore模块下的getters映射为组件实例的属性 */
    ...mapGetters('departmentsStore', ['treeDepartments'])
  },
  mounted() {
    /* 组件挂载完毕调用fetchDepartments()派发action发送请求获取部门列表 */
    // this.$store.dispatch('departmentsStore/fetchDepartments')
  }
}
</script>

<template>
  <el-dialog v-bind="$attrs" :visible="showEmpDialog" @close="handleClose">
    <el-form ref="form" label-width="100px" :model="formData" :rules="formRules">
      <el-form-item label="员工姓名" prop="username">
        <el-input placeholder="请输入员工姓名" v-model="formData.username"></el-input>
      </el-form-item>
      <el-form-item label="员工手机" prop="mobile">
        <el-input placeholder="请输入员工手机" v-model="formData.mobile"></el-input>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker type="date" placeholder="请选择入职日期" v-model="formData.timeOfEntry"></el-date-picker>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker type="date" placeholder="请选择转正日期" v-model="formData.correctionTime"></el-date-picker>
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select placeholder="请选择聘用形式" v-model.number="formData.formOfEmployment">
          <el-option v-for="(item,index) in employmentType" :value="item.id" :label="item.value" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="员工工号" prop="workNumber">
        <el-input placeholder="请输入员工工号" v-model="formData.workNumber"></el-input>
      </el-form-item>
      <el-form-item label="所属部门" prop="departmentName">
        <el-input placeholder="请选择部门名称" v-model="formData.departmentName" @focus="handleFocus"></el-input>
        <el-tree v-if="isShowTree" :data="treeDepartments" :props="defaultProps" @node-click="handleNodeClick" :expand-on-click-node="false" check-strictly></el-tree>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
    <el-button type="danger" @click="handleCancel">取 消</el-button>
    <el-button type="primary" @click="handleSubmit">确 定</el-button>
  </span>
  </el-dialog>

</template>

<style scoped lang="scss">
::v-deep .el-dialog {
  .el-dialog__body {
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .el-form {
    width: 70%;

    .el-date-editor {
      width: 100%;
    }

    .el-select {
      width: 100%;
    }
  }
}


</style>
